///*
// *  Copyright 2020-2021 Li Fei
// *
// *  Licensed under the Apache License, Version 2.0 (the "License");
// *  you may not use this file except in compliance with the License.
// *  You may obtain a copy of the License at
// *
// *  http://www.apache.org/licenses/LICENSE-2.0
// *
// *  Unless required by applicable law or agreed to in writing, software
// *  distributed under the License is distributed on an "AS IS" BASIS,
// *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// *  See the License for the specific language governing permissions and
// *  limitations under the License.
// */
//package com.pengshun.modules.security.service;
//
//import com.pengshun.modules.api.coal.service.dto.UserDto;
//import com.pengshun.modules.api.user.service.UserService;
//import lombok.RequiredArgsConstructor;
//import com.pengshun.exception.BadRequestException;
//import com.pengshun.exception.EntityNotFoundException;
//import com.pengshun.modules.security.config.bean.LoginProperties;
//import com.pengshun.modules.security.service.dto.JwtUserDto;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//
//import javax.persistence.EntityNotFoundException;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * @author Li Fei
// * @date 2020年10月10日
// */
//@RequiredArgsConstructor
//@Service("userDetailsService")
//public class UserDetailsServiceImpl implements UserDetailsService {
//    private final UserService userService;
////    private final RoleService roleService;
////    private final DataService dataService;
//    private final LoginProperties loginProperties;
//    public void setEnableCache(boolean enableCache) {
//        this.loginProperties.setCacheEnable(enableCache);
//    }
//
//    /**
//     * 用户信息缓存
//     *
//     * @see {@link UserCacheClean}
//     */
//    static Map<String, JwtUserDto> userDtoCache = new ConcurrentHashMap<>();
//
//    @Override
//    public JwtUserDto loadUserByUsername(String username) {
//        boolean searchDb = true;
//        JwtUserDto jwtUserDto = null;
//        if (loginProperties.isCacheEnable() && userDtoCache.containsKey(username)) {
//            jwtUserDto = userDtoCache.get(username);
//            searchDb = false;
//        }
//        if (searchDb) {
//            UserDto user;
//            try {
//                user = userService.findByName(username);
//            } catch (EntityNotFoundException e) {
//                // SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException
//                throw new UsernameNotFoundException("", e);
//            }
//            if (user == null) {
//                throw new UsernameNotFoundException("");
//            } else {
//                if (!user.getEnabled()) {
//                    throw new Exception("账号未激活！");
//                }
//                jwtUserDto = new JwtUserDto(
//                        user,null,null
////                        dataService.getDeptIds(user),
////                        roleService.mapToGrantedAuthorities(user)
//                );
//                userDtoCache.put(username, jwtUserDto);
//            }
//        }
//        return jwtUserDto;
//    }
//}
